package com.education.wisdom.controller.management;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.StringUtils;
import com.education.wisdom.pojo.CatalogFree;
import com.education.wisdom.pojo.Microlesson;
import com.education.wisdom.service.ICatalogFreeService;
import com.education.wisdom.service.IMicrolessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 免费公开课--资源管理
 */
@RestController
@RequestMapping("/Manage/Microlesson")
public class MicrolessonController {

    @Autowired
    private IMicrolessonService microlessonService;

    @Autowired
    private ICatalogFreeService catalogFreeService;

    //查询
    @GetMapping("/selectAll")
    public ApiReturnObject selectAll(Microlesson microlesson, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<Microlesson> microlessonList = microlessonService.selectMicrolessonList(microlesson);
        for (Microlesson micro : microlessonList) {
            if (micro.getRCatalogid() != null && !"".equals(micro.getRCatalogid())) {
                CatalogFree catalogFree = catalogFreeService.selectByCatalogId(Integer.parseInt(micro.getRCatalogid()));
                if (StringUtils.isNotNull(catalogFree))micro.setParentName(catalogFree.getName());
            }
        }
        PageInfo<Microlesson> pageInfo = new PageInfo<>(microlessonList);
        return new ApiReturnObject("200", "操作成功", pageInfo);
    }

    //修改
    @PostMapping("/update")
    public ApiReturnObject update(@RequestBody Microlesson microlesson) {
        microlessonService.updateMicrolesson(microlesson);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }

    //添加
    @PostMapping("/insert")
    public ApiReturnObject ins(@RequestBody Microlesson microlesson) {
        microlesson.setCreateTime(new Date());
        microlessonService.insertMicrolesson(microlesson);
        return new ApiReturnObject("200", "操作成功", "操作成功");
    }

    //删除
    @GetMapping("/delete")
    public ApiReturnObject delete(String ids) {
        int pageSize = microlessonService.deleteMicrolessonByIds(ids);
        if (pageSize > 0) {
            return new ApiReturnObject("200", "操作成功", "操作成功");
        }
        return new ApiReturnObject("201", "炒作失败", "炒作失败");
    }
}
